/**     
* @Title: HTMLCleanUtil.java   
* @Package me.crawler.util
* @Description: TODO   
* @author xaoyaoyao
* @date 2019年1月12日 下午1:17:36
*/
package me.crawler.util;

import java.io.IOException;

import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.SimpleXmlSerializer;
import org.htmlcleaner.TagNode;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: HTMLCleanUtil
 * @Description: TODO
 * @author xaoyaoyao
 * @date 2019年1月12日 下午1:17:36
 */
@Slf4j
public class HTMLCleanUtil {

	private static HtmlCleaner cleaner = new HtmlCleaner();

	private static CleanerProperties props = new CleanerProperties();

	static {
		props = new CleanerProperties();
		props.setAllowHtmlInsideAttributes(true);
		props.setOmitComments(true);
		props.setOmitDoctypeDeclaration(true);
		props.setOmitXmlDeclaration(true);
		props.setPruneTags("script,link,base,style,meta");
		props.setAdvancedXmlEscape(true);
		cleaner = new HtmlCleaner(props);
	}

	@SuppressWarnings("deprecation")
	public static String clean(String orgPage) {
		String source = null;
		try {
			TagNode rootNode = cleaner.clean(orgPage);
			SimpleXmlSerializer xmlSerializer = new SimpleXmlSerializer(props);
			source = xmlSerializer.getXmlAsString(rootNode);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}

		return source;
	}

	@SuppressWarnings("deprecation")
	public static String cleanHtml(String htmlPage) throws IOException {
		CleanerProperties props = new CleanerProperties();
		props.setAllowHtmlInsideAttributes(true);
		props.setOmitComments(true);
		props.setOmitDoctypeDeclaration(true);
		props.setPruneTags("script,link,base,style,meta");
		props.setAdvancedXmlEscape(true);
		HtmlCleaner cleaner = new HtmlCleaner(props);
		SimpleXmlSerializer simpleXmlSerializer = new SimpleXmlSerializer(props);
		// htmlPage = HtmlUtils.htmlUnescape(htmlPage);
		TagNode tagNode = cleaner.clean(htmlPage);
		return simpleXmlSerializer.getXmlAsString(tagNode);
	}

	public static void clean(String inHtmlFileName, String outXmlFileName) {
		// // take default cleaner properties
		// CleanerProperties props = new CleanerProperties();
		//
		// // customize cleaner's behaviour with property setters
		// props.setAllowHtmlInsideAttributes(true);
		// props.setOmitComments(true);
		// props.setOmitDoctypeDeclaration(true);
		// props.setOmitXmlDeclaration(true);
		// // props.setTreatUnknownTagsAsContent(true);
		// // props.setOmitDeprecatedTags(true);
		// props.setPruneTags("script,link,base,style,meta");
		// props.setAdvancedXmlEscape(true);
		//
		// // create an instance of HtmlCleaner
		// HtmlCleaner cleaner = new HtmlCleaner(props);
		//
		// try {
		// // Clean HTML taken from simple string, file, URL, input stream,
		// // input source or reader. Result is root node of created
		// // tree-like structure. Single cleaner instance may be safely used
		// // multiple times.
		// TagNode rootNode = cleaner.clean(new File(inHtmlFileName));
		// SimpleXmlSerializer xmlSerializer = new SimpleXmlSerializer(props);
		// xmlSerializer.writeXmlToFile(rootNode, outXmlFileName, "utf-8");
		// } catch (IOException e) {
		// logger.error(e.getMessage(), e);
		// }
	}
}
